﻿// <copyright file="Vector3DCollectionConverter.cs" company="Axelerate Solutions S.A.">
// Copyright (c) Axelerate Solutions S.A.  All rights reserved.
// </copyright>

namespace Axelerate.Silverlight3D.Media.Media3D
{
    using System;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Ink;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using System.ComponentModel;

    
    /// <summary>
    /// File name: Vector3DCollectionConverter.cs
    /// Company: Axelerate Solutions S.A.
    /// Created By: Daniel Rojas
    /// Created Date: 10/07/2008 12:23:15 h.
    /// Description: Converts instances of other types to and from Vector3DCollection instances.
    ///   <Modifications>
    ///   </Modifications>
    /// </summary>
    public class Vector3DCollectionConverter : TypeConverter
    {
        /// <summary>
        /// Returns a value that indicates whether the type converter can convert from a specified type.
        /// </summary>
        /// <param name="context">The type descriptor context for this call.</param>
        /// <param name="sourceType">The type being queried for support.</param>
        /// <returns>true if the converter can convert from the provided type; otherwise, false.</returns>
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            if (sourceType.Equals(typeof(string)))
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// Returns a value that indicates whether the type converter can convert to the specified type.
        /// </summary>
        /// <param name="context">The type descriptor context for this call.</param>
        /// <param name="destinationType">The type being queried for support.</param>
        /// <returns>true if the converter can convert to the provided type; otherwise, false.</returns>
        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            if (destinationType.Equals(typeof(string)))
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// Attempts to convert to a Vector3DCollection from the given object.
        /// </summary>
        /// <param name="context">The type descriptor context for this call.</param>
        /// <param name="culture">The culture information that is respected when converting.</param>
        /// <param name="value">The object to convert to an instance of Vector3DCollection.</param>
        /// <returns>The Vector3DCollection that was constructed.</returns>
        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
        {
            if (value is string)
            {
                if (value.ToString().Contains(","))
                {
                    return Vector3DCollection.Parse((string)value);
                }
                else
                {
                    Vector3DCollection col = new Vector3DCollection();
                    string[] vectorsdata = value.ToString().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    for (int i = 0; i < vectorsdata.Length; i += 3)
                    {
                        col.Add(new Vector3D(double.Parse(vectorsdata[i]), double.Parse(vectorsdata[i + 1]), double.Parse(vectorsdata[i + 2])));
                    }
                    return col;
                }
            }
            return null;
        }

        /// <summary>
        /// Attempts to convert an instance of Vector3DCollection to the specified type
        /// </summary>
        /// <param name="context">The type descriptor context for this call.</param>
        /// <param name="culture">The culture information that is respected when converting.</param>
        /// <param name="value">The object to convert to an instance of destinationType.</param>
        /// <param name="destinationType">Type to which this method will convert the Vector3DCollection instance.</param>
        /// <returns>The object that was constructed.</returns>
        public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
        {
            if (value is Vector3DCollection && destinationType.Equals(typeof(string)))
            {
                return value.ToString();
            }
            return null;
        }
    }
}
